﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneShoppingDAL;
using OneShoppingBLL;
using OneShoppingUtility;
using OneShoppingModel;

public partial class Backstage_Pages_sysmng_menuInfoAlter : BasePage
{
    int menuid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (role.RoleId != 1)
        {
            Response.Redirect("login.html");
        }
        if(!IsPostBack)
        {
            BindDDL();
            try
            {
                menuid = Convert.ToInt32(Request["id"]);
                Session["menuid"] = menuid;
            }
            catch 
            {
                UtilityMessageBox.ShowAndRedirect(this, "非法访问", "menuManager.aspx");
                return;
            }
            var m = MenuBLL.GetInstance().GetEntity(r => r.MenuId == menuid);
            menuname.Text = m.MenuName;
            menuurl.Text = m.MenuUrl;
            if(m.ParentMenu!=0)
                menulist.SelectedValue = m.ParentMenu.ToString();
            menuseq.Text = m.MenuDispSeq.ToString();
        }
        else
        {
            menuid =Convert.ToInt32(Session["menuid"]);
        }
    }

    public void BindDDL()
    {
        UtilityDataBind.DropDownListBind<OneShoppingModel.Menu>(ref menulist,
            MenuBLL.GetInstance().GetList(r => r.ParentMenu == 0), "MenuName", "MenuId", "一级菜单", "0");
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (menuname.Text.Trim().Length == 0)
        {
            msg_name.InnerText = "菜单名称不能为空";
            msg_name.Visible = true;
            return;
        }
        if (menuseq.Text.Trim().Length == 0)
        {
            msg_seq.InnerText = "权值不能为空";
            msg_seq.Visible = true;
            return;
        }
        int mseq;
        try
        {
            mseq = Convert.ToInt32(menuseq.Text);
        }
        catch 
        {
            msg_seq.InnerText = "权值必须为数字";
            msg_seq.Visible = true;
            return;
        }
        var m = MenuBLL.GetInstance().GetEntity(r => r.MenuId == menuid);
        m.MenuName = menuname.Text;
        m.MenuDispSeq = mseq;
        m.MenuUrl = menuurl.Text;
        m.ParentMenu =Convert.ToInt32(menulist.SelectedValue);
        if (m.MenuUrl.Trim().Length == 0)
            m.MenuUrl = "#";
        bool rm = MenuBLL.GetInstance().Update(m);
        if (rm)
        {
            UtilityMessageBox.ShowAndRedirect(this, "更新成功", "menuManager.aspx");
        }
        else
        {
            UtilityMessageBox.Show(this, "更新失败");
        }
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("menuManager.aspx");
    }
}